package com.easemob.chatuidemo.adapter;

import java.util.List;

import cn.sharesdk.framework.authorize.h;

import com.amap.api.a.an;
import com.easemob.chat.EMContactManager;
import com.easemob.exceptions.EaseMobException;
import com.hcinfos.gsoo.R;
import com.hcinfos.gsoo.enity.IMUser;
import com.hcinfos.gsoo.enity.User;
import com.hcinfos.gsoo.util.SharePreferenceUtil;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class SearchAdapter extends BaseAdapter {

	private Context context;
	private List<IMUser> list;
	private LayoutInflater mInflater;
	private SharePreferenceUtil util;
	private User loginUser;

	public SearchAdapter(Context context, List<IMUser> list) {
		mInflater = LayoutInflater.from(context);
		this.context = context;
		this.list = list;
		util = new SharePreferenceUtil(context, "setting");
		loginUser = (User) util.loadObject("user");
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final Holder holder;
		if (convertView == null) {
			holder = new Holder();
			convertView = mInflater.inflate(R.layout.adapter_im_search_item,
					null);
			convertView.setTag(holder);
			holder.add_btn = (Button) convertView.findViewById(R.id.add_btn);
			holder.name_tv = (TextView) convertView.findViewById(R.id.name_tv);
			holder.msg_tv = (TextView) convertView.findViewById(R.id.msg_tv);
		} else {
			holder = (Holder) convertView.getTag();
		}

		final IMUser user = list.get(position);
		holder.name_tv.setText(user.getGgnumber());
		if (user.isFriend()) {
			holder.add_btn.setVisibility(View.GONE);
			holder.msg_tv.setText("已是你好友");
			holder.msg_tv.setVisibility(View.VISIBLE);
		} else if (loginUser.getGgnumber().equals(user.getGgnumber())) {
			holder.add_btn.setVisibility(View.GONE);
			holder.msg_tv.setText("自己");
			holder.msg_tv.setVisibility(View.VISIBLE);
		} else {
			holder.add_btn.setVisibility(View.VISIBLE);
			holder.msg_tv.setVisibility(View.GONE);

		}
		holder.add_btn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				try {
					EMContactManager.getInstance().addContact(
							"X" + user.getGgnumber(),
							user.getGgnumber() + "请求加你为好友");
					holder.add_btn.setVisibility(View.GONE);
					holder.msg_tv.setVisibility(View.VISIBLE);
					holder.msg_tv.setText("已发送");
				} catch (EaseMobException e) {
					Log.e("wxf", "e" + e.toString());
					Toast.makeText(context, "添加失败", 500).show();
				}
			}
		});
		return convertView;
	}

	public final class Holder {

		TextView name_tv;
		Button add_btn;
		TextView msg_tv;
	}

	public void setDataChange(List<IMUser> list) {

		this.list = list;
		notifyDataSetChanged();
	}

}
